package com.study.common;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

public class QueueTest {


    public QueueTest() {
        System.out.println("无参构造函数被调用");
    }

    private static LinkedBlockingQueue<Integer> linkedBlockingQueue = new LinkedBlockingQueue<>(10);
    private static List<Integer> integers = new ArrayList<>();

    public static void main(String[] args) {
        ThreadPoolExecutor threadPoolExecutor = new ThreadPoolExecutor
                (100,150,20,
                        TimeUnit.SECONDS,new LinkedBlockingQueue<>(),
                        new ThreadPoolExecutor.AbortPolicy());

        for (int i = 0; i <150 ; i++) {
            threadPoolExecutor.execute(()->{
                for (int j = 0; j <10 ; j++) {
                    integers.add(j);
                }
            });
        }


        System.out.println(threadPoolExecutor.getActiveCount());
        while (threadPoolExecutor.getActiveCount()==0){

            System.out.println(integers);
            System.out.println(integers.size());
            break;
        }



        new Thread(() -> {
            for (int i = 0; i < 10; i++) {
                integers.add(i);
            }
        }).start();

        new Thread(() -> {
            for (int i = 0; i < 10; i++) {
                integers.add(i);
            }
        }).start();


        new Thread(() -> {
            for (int i = 0; i < 10; i++) {
                integers.add(i);
            }
        }).start();


        try {
            Thread.sleep(2000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }


        //System.out.println(integers);


        new Thread(() -> {
            for (int i = 0; i <= 20; i++) {
                try {
                    linkedBlockingQueue.put(i);
                    System.out.println(Thread.currentThread().getName() + "线程添加元素" + i);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }, "生产线程1").start();


        new Thread(() -> {
            for (int i = 0; i <= 20; i++) {
                try {
                    linkedBlockingQueue.put(i);
                    System.out.println(Thread.currentThread().getName() + "线程添加元素" + i);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }, "生产线程2").start();


        new Thread(() -> {
            for (int i = 0; i < 40; i++) {
                try {
                    Thread.sleep(200);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                System.out.println("消费线程第" + i + "次消费" + Thread.currentThread().getName() + "线程消费=======================" + QueueTest.linkedBlockingQueue.poll());
            }
        }, "消费线程").start();
    }
}

